


void kahansum(float *sum,float* eps,float x)
{
	float y, t;
	y = x - *eps;
	t = *sum + y;
	*eps = (t - *sum) - y;
	*sum = t;
}

#if 0
#include <stdio.h>
#define TIMES 1000000
int main(int argc, char* argv[])
{
    int i;
    float x=0.001f;
    float sum1,sum2,sum3;
    float eps=0;
 
 
    sum1=0.0f;
    sum2=0.0f;
 
    for(i=0;i<TIMES;i++)
    {
		float y,t;
        sum1+=x;
        y=x-eps;
        t=sum2+y;
        eps=(t-sum2)-y;
        sum2=t;
		//printf("%d\t%f\t%f\n",i,sum2,eps);
    }
    sum3=0.0f;
	eps = 0.0f;
    for(i=0;i<TIMES;i++)
    {
		KahanSum(&sum3,&eps,x);
		//printf("%d\t%f\t%f\n",i,sum3,eps);
	}
    printf("\n理论值：%f\n",x*TIMES);
    printf("\n累加值：%f\n",sum1);
    printf("\nKahan累加值：%f\n",sum2);
    printf("\nmy Kahan累加值：%f\n",sum3);
 
    return 0;
}
#endif
